# app/services/friend_service.py
from app import db
from flask import current_app,request
import os
import shutil
import uuid
from datetime import datetime
from app.models import Playlist

class MusicService:

    @staticmethod
    def get_play_lists(user_id:str):
        try:
            playLists = Playlist.query.filter_by(user_id=user_id).all()
            playListArr = []
            for playList in playLists:
                print(playList.playlist_id)
                playListArr.append({
                    'playlist_id': playList.playlist_id,
                    'user_id': playList.user_id,
                    'name': playList.name,
                    'description': playList.description or None,
                    'cover_url': playList.cover_url or None,
                    'is_public': playList.is_public,
                    'created_at': playList.created_at,
                    'updated_at': playList.updated_at,
                    'songs': playList.songs or None,
                })

            return playListArr
        except Exception as e:
            print(f"获取歌单列表失败: {str(e)}")
            return f'获取歌单列表失败', 500

    @staticmethod
    def add_play_list(user_id:str,name:str):
        try:
            playlist = Playlist(
                playlist_id = str(uuid.uuid4()),
                user_id = user_id,
                name = name,
                is_public = False,
            )
            db.session.add(playlist)
            db.session.commit()

            return '创建歌单成功'
        except Exception as e:
            print(f"创建歌单列表失败: {str(e)}")
            return f'创建歌单列表失败', 500

    @staticmethod
    def edit_play_list(user_id:str,playlist_id:str,name:str,cover_url:str,descripe:str,songs:str):
        try:
            playlist = Playlist.query.filter_by(playlist_id=playlist_id).first()
            if name:
                playlist.name = name
            if cover_url:
                playlist.cover_url = cover_url
            if descripe:
                playlist.description = descripe
            if songs:
                playlist.songs = songs

            db.session.commit()

            return '编辑歌单成功'
        except Exception as e:
            print(f"编辑歌单列表失败: {str(e)}")
            return f'编辑歌单列表失败', 500